package com.cheng.admin.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cheng.admin.constant.Constants;
import com.cheng.admin.model.Coupon;
import com.cheng.admin.model.CouponUser;
import com.cheng.admin.service.CouponService;
import com.cheng.admin.service.CouponUserService;
import com.cheng.common.domain.BaseSearchVO;
import com.cheng.common.domain.R;
import com.cheng.common.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 这个目前不用看
 * <p>
 * 优惠券表 前端控制器
 * </p>
 *
 * @author 起凡
 * @since 2022-01-17
 * @deprecated 这个目前不用看
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    CouponService couponService;
    @Autowired
    CouponUserService couponUserService;

    @PostMapping("save")
    public R<Boolean> save(@RequestBody Coupon coupon) {
        Boolean check1 = (coupon.getCouponType().equals(Constants.COUPON_TYPE_FULL_REDUCE) && coupon.getDiscount() != null);
        Boolean check2 = (coupon.getCouponType().equals(Constants.COUPON_TYPE_DISCOUNT) && coupon.getMoney() != null);
        assert check1 || check2;//有一个条件满足就说明优惠券有效
        couponService.saveOrUpdate(coupon);
        return R.ok(true);
    }

    @GetMapping("send")
    public R<Boolean> sendCoupon(@RequestParam Long userId, @RequestParam Long couponId) {
        CouponUser couponUser = new CouponUser();
        couponUser.setUserId(userId);
        couponUser.setCouponId(couponId);
        couponUserService.save(couponUser);
        return R.ok(true);
    }

    @GetMapping("receive")
    public R<Boolean> receive(@RequestParam Long couponId) {
//TODO 隐藏bug多人领取,需要加一个version
        CouponUser one = couponUserService.getOne(Wrappers.<CouponUser>lambdaQuery().eq(CouponUser::getCouponId, couponId));
        if (one != null) {
            throw new CustomException("请勿重复领取该优惠券");
        }
        Coupon coupon = couponService.getOne(Wrappers.<Coupon>lambdaQuery().eq(Coupon::getId, couponId).gt(Coupon::getLastTotal, 0));
        if (coupon == null) {
            throw new CustomException("该优惠券已被领取完");
        }
        CouponUser couponUser = new CouponUser();
        couponUser.setUserId(StpUtil.getLoginIdAsLong());
        couponUser.setCouponId(couponId);
        couponUserService.save(couponUser);
//        剩余数量减少
        coupon.setLastTotal(coupon.getLastTotal() - 1);
        couponService.updateById(coupon);
        return R.ok(true);
    }

    @GetMapping("delete")
    public R<Boolean> delete(@RequestParam Long couponId) {
        couponService.removeById(couponId);
        return R.ok(true);
    }

    @GetMapping("list")
    public R<PageInfo<Coupon>> list(BaseSearchVO baseSearchVO) {
        PageHelper.startPage(baseSearchVO.getPageNum(), baseSearchVO.getPageSize());
        List<Coupon> list = couponService.list();
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(list);
        return R.ok(couponPageInfo);
    }

    @GetMapping("listReceiveCoupon")
    public R<PageInfo<Coupon>> listReceiveCoupon(BaseSearchVO baseSearchVO) {
        PageHelper.startPage(baseSearchVO.getPageNum(), baseSearchVO.getPageSize());
        List<Coupon> list = couponService.list(Wrappers.<Coupon>lambdaQuery().gt(Coupon::getLastTotal, 0));
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(list);
        return R.ok(couponPageInfo);
    }
}

